package com.paomi.client.view.svg;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


import com.paomi.client.activity.detail.model.OnlineSiteBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhangjd on 2017/6/1.
 * https://github.com/zhangjundi/taiwanDemo
 * svg的加载模式
 */

public class MapView extends View implements ParserCallBack {
    private List<OnlineSiteBean.SectionBean> list=new ArrayList<>();
    private ViewAttr mViewAttr;
    private Paint mPaint;
    private Path mPath;
    private float scale = 0.5f;
    private int viewWidth;
    private int viewHeight;
    //是否计算完成
    private boolean isCalculation;
    ParserCallBack mParserCallBack;
    private float aspectRatioWidth = 1;
    private float aspectRatioHeight = 1;
    private float mViewportWidth;
    private float mViewportHeight;
    private PointF mViewport = new PointF(mViewportWidth, mViewportHeight);
    private Activity mContext;

    public MapView(Context context) {
        super(context);
    }

    public MapView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
//        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
//        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
//        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
//        setMeasuredDimension(getMeasureSize(300, widthMeasureSpec),
//                getMeasureSize(300, heightMeasureSpec));
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (height <= 0 && width <= 0 && heightMode == MeasureSpec.UNSPECIFIED &&
                widthMode == MeasureSpec.UNSPECIFIED) {
            width = 0;
            height = 0;
        } else if (height <= 0 && heightMode == MeasureSpec.UNSPECIFIED) {
            height = (int) (width * aspectRatioHeight / aspectRatioWidth);
        } else if (width <= 0 && widthMode == MeasureSpec.UNSPECIFIED) {
            width = (int) (height * aspectRatioWidth / aspectRatioHeight);
        } else if (width * aspectRatioHeight > aspectRatioWidth * height) {
            width = (int) (height * aspectRatioWidth / aspectRatioHeight);
        } else {
            height = (int) (width * aspectRatioHeight / aspectRatioWidth);
        }

        super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST),
                MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST));
    }


    private int getMeasureSize(int defultSize, int measureSpce) {
        int mode = MeasureSpec.getMode(measureSpce);
        int size = MeasureSpec.getSize(measureSpce);
        int measureSize = defultSize;
        switch (mode) {
            case MeasureSpec.EXACTLY:
                measureSize = Math.max(defultSize, size);
                break;
            case MeasureSpec.AT_MOST:
                measureSize = defultSize;
                break;
        }
        return measureSize;
    }


    public void init(List<OnlineSiteBean.SectionBean> list, Activity context) {
        mContext=context;
        this.list=list;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
//        invalidate();
        //解析svg xml
//        SVGXmlParserUtils.parserXml(getResources().openRawResource(R.raw.taiwan), this);
        mViewport = new PointF(mViewportWidth, mViewportHeight);
        mParserCallBack=this;
        mViewAttr=new ViewAttr();
        if(mParserCallBack!=null){
            mParserCallBack.callback(list,mViewAttr);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
        myDraw();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (list == null) {
            return;
        }
        float X = viewWidth / mViewport.x;
        float Y = viewHeight / mViewport.y;

        Matrix scaleMatrix = new Matrix();
        RectF outerRect = new RectF(X, X, Y, Y);
        scaleMatrix.setScale(X, Y, outerRect.centerX(), outerRect.centerY());
        //        Matrix mMatrix = new Matrix();
//        mMatrix.postScale(0.5f,0.5f);
        //这个set方法不可以
//        mMatrix.setScale(0.5f,0.5f);
//        canvas.concat(mMatrix);
        //上面的方法也可以
//        canvas.restore();
        canvas.scale(X, Y);
        canvas.drawColor(Color.TRANSPARENT);
//        canvas.translate(X,Y);
        for (int i = 0; i < list.size(); i++) {
            OnlineSiteBean.SectionBean path = list.get(i);
            //绘制边的颜色
            mPaint.setStrokeWidth(2);
            mPaint.setStyle(Paint.Style.FILL);
            if(path.getFillcolor().length()>4){
                mPaint.setColor(Color.parseColor(path.getFillcolor()));
            }else{
                mPaint.setColor(Color.parseColor("#ffffff"));
            }
//            mPaint.setStrokeWidth(
//                    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
            RectF mRectF=new RectF();
            //处理path的边界
            //计算控制点的边界
            path.getmPath().computeBounds(mRectF, true);
            ViewAttr mViewAttr=new ViewAttr();
            mViewAttr.colSize(mRectF);
            canvas.drawPath(path.getmPath(), mPaint);
        }
        if (mPath != null) {
            mPaint.setStrokeWidth(2);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(Color.parseColor("#EA4335"));
            canvas.drawPath(mPath, mPaint);
        }

    }

    @Override
    public void callback(List<OnlineSiteBean.SectionBean> list, ViewAttr mViewAttr) {
        this.list = list;
        this.mViewAttr = mViewAttr;
        myDraw();
    }
    /**
     * If you set the SVG data paths more than once using {@link #( String...)} you should call this method
     * before playing the animation.
     */
    @SuppressWarnings("SuspiciousNameCombination")
    public void rebuildGlyphData() {

        float X = viewWidth / mViewport.x;
        float Y = viewHeight / mViewport.y;

        Matrix scaleMatrix = new Matrix();
        RectF outerRect = new RectF(X, X, Y, Y);
        scaleMatrix.setScale(X, Y, outerRect.centerX(), outerRect.centerY());


    }

    private void myDraw(){
        if(!isCalculation&&mViewAttr!=null) {
            if (mViewAttr.getWidth() > 0 && mViewAttr.getHeight() > 0 && viewWidth > 0 && viewHeight > 0) {
                isCalculation = true;
                float widthScale = viewWidth * 1.00f / mViewAttr.getWidth();
                float heightScale = viewHeight * 1.00f / mViewAttr.getHeight();
                scale = Math.min(widthScale, heightScale);
            }
            postInvalidate();
        }
    }

    String url = null;
    float x;
    float y;
    float X;
    float Y;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        if (event.getAction() == MotionEvent.ACTION_DOWN) {
//            float x = event.getX();
//            float y = event.getY();
//            float X = viewWidth / mViewport.x;
//            float Y = viewHeight / mViewport.y;
//            if (list != null)
//                for (int i = 0; i < list.size(); i++) {
//                    VenueData cityPath = list.get(i);
//                    if (cityPath.isArea(x / X, y / Y)) {
//                        mPath = cityPath.getmPath();
//                        postInvalidate();
//                        Intent mIntent = new Intent(mContext, GoogleVRActivity.class);
//                        mIntent.putExtra("venueImg", cityPath.getImg_url()==null?"":cityPath.getImg_url());
//                        mContext.startActivity(mIntent);
//                        mContext.overridePendingTransition(R.anim.anim_setin, R.anim.anim_setout);
//                        break;
//                    }
//                }
//        }
//        return super.onTouchEvent(event);


        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            x = event.getX();
            y = event.getY();
            X = viewWidth / mViewport.x;
            Y = viewHeight / mViewport.y;

        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (list != null)
                for (int i = 0; i < list.size(); i++) {
                    OnlineSiteBean.SectionBean cityPath = list.get(i);
//                    if (cityPath.isArea(x / X, y / Y)) {
//                        mPath = cityPath.getmPath();
//                        postInvalidate();
//                        Intent mIntent = new Intent(mContext, GoogleVRActivity.class);
//                        mIntent.putExtra("venueImg", cityPath.getImg_url() == null ? "" : cityPath.getImg_url());
//                        mContext.startActivity(mIntent);
//                        mContext.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//                        break;
//                    }else if(cityPath.isArea(x / scale, y / scale)){
//                        mPath = cityPath.getmPath();
//                        postInvalidate();
//                        RxToast.info(mContext,"内容尚未加载完成");
//                        break;
//                    }
                }
        }
        return true;


    }
    /**
     * Set the viewport width and height of the SVG. This can be found in the viewBox in the SVG. This is not the size
     * of the view.
     *
     * @param viewportWidth
     *     the width
     * @param viewportHeight
     *     the height
     */
    public void setViewportSize(float viewportWidth, float viewportHeight) {
        mViewportWidth = viewportWidth;
        mViewportHeight = viewportHeight;
        aspectRatioWidth = viewportWidth;
        aspectRatioHeight = viewportHeight;
//        mViewport = new PointF(mViewportWidth, mViewportHeight);
        requestLayout();
    }
}
